package com.example.travel_front.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.travel_front.Activity.SearchActivity;
import com.example.travel_front.Adapter.NearbyTravelerAdapter;
import com.example.travel_front.Adapter.PopularPlaceAdapter;
import com.example.travel_front.Model.PopularPlace;
import com.example.travel_front.Model.Traveler;
import com.example.travel_front.R;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_index, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RecyclerView rvNearbyTravelers = view.findViewById(R.id.rvNearbyTravelers);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        rvNearbyTravelers.setLayoutManager(layoutManager);

        List<Traveler> travelers = getTravelers();
        NearbyTravelerAdapter adapter1 = new NearbyTravelerAdapter(travelers);
        rvNearbyTravelers.setAdapter(adapter1);

        RecyclerView rvPopularPlaces = view.findViewById(R.id.rvPopularPlaces);
        LinearLayoutManager layoutManagerPopular = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        rvPopularPlaces.setLayoutManager(layoutManagerPopular);

        List<PopularPlace> popularPlaces = getPopularPlaces();
        PopularPlaceAdapter adapter2 = new PopularPlaceAdapter(popularPlaces);
        rvPopularPlaces.setAdapter(adapter2);

        EditText searchEditText = view.findViewById(R.id.searchEditText);
        searchEditText.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                return true;
            }
            return false;
        });

    }

    private List<Traveler> getTravelers() {
        List<Traveler> travelers = new ArrayList<>();

        // 添加一些假数据
        travelers.add(new Traveler("Alice Smith", "https://jdhn.oss-cn-hangzhou.aliyuncs.com/avatar1.png", 120, 250));
        travelers.add(new Traveler("Bob Johnson", "https://jdhn.oss-cn-hangzhou.aliyuncs.com/avatar2.png", 85, 190));
        travelers.add(new Traveler("Charlie Williams", "https://jdhn.oss-cn-hangzhou.aliyuncs.com/avatar3.png", 200, 300));
        travelers.add(new Traveler("Diana Brown", "https://jdhn.oss-cn-hangzhou.aliyuncs.com/avatar4.png", 75, 110));


        return travelers;
    }

    // 获取 "Popular Places" 数据的方法
    private List<PopularPlace> getPopularPlaces() {
        List<PopularPlace> popularPlaces = new ArrayList<>();

        // 添加一些假数据
        popularPlaces.add(new PopularPlace("https://example.com/place1.png", "NEW YORK",827366));

        return popularPlaces;
    }
}
